package pages;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

/**
 * Help utilities for Lab-Management the system
 * 
 * 
 */
public class LMUtils {

	private LMUtils() {
	};

	/**
	 * Searches for the parameter in ServletContext
	 * 
	 * @param parameterName
	 * @return the value of the parameter
	 * @throws DBException
	 *             if the parameter is not found
	 */
	public static String getContextParameterValue(
			ServletContext servletContext, String parameterName)
			throws InternalErrorException {
		String parameterValue = servletContext.getInitParameter(parameterName);
		if (parameterValue == null) {
			throw new InternalErrorException("context-param " + parameterName
					+ " was not defined in web.xml");
		}
		return parameterValue;
	}

	/**
	 * Searches for the parameter in request
	 * 
	 * @param request
	 * @param parameterName
	 * @return the value of the parameter
	 * @throws MissingParameterException
	 *             if the parameter is not set
	 */
	public static String getRequestParameterValue(HttpServletRequest request,
			String parameterName) throws MissingParameterException {
		String[] parameterValues = request.getParameterValues(parameterName);
		if (parameterValues == null || parameterName.length() < 1) {
			throw new MissingParameterException(parameterName);
		}
		return parameterValues[0];
	}

	/**
	 * Searches for the parameter in request
	 * 
	 * @param request
	 * @param parameterName
	 * @return the value of the parameter
	 * @throws MissingParameterException
	 *             if the parameter is not set or empty
	 */
	public static String getRequestParameterValueNotEmpty(
			HttpServletRequest request, String parameterName)
			throws MissingParameterException {
		String parameterValue = getRequestParameterValue(request, parameterName);
		if (parameterValue.isEmpty()) {
			throw new MissingParameterException(parameterName);
		}
		return parameterValue.trim();
	}

	/**
	 * Sets string into the session
	 * 
	 * @param request
	 * @param parameterName
	 * @param value
	 */
	public static void setSessionParamenterValue(HttpServletRequest request,
			String parameterName, Object value) {
		request.getSession().setAttribute(parameterName, value.toString());
	}

	/**
	 * Searches for the parameter in session and converts it to string
	 * 
	 * @param request
	 * @param parameterName
	 * @return the string if found
	 */
	public static String getSessionParamenterValue(HttpServletRequest request,
			String parameterName) {
		Object value = request.getSession().getAttribute(parameterName);
		if (value == null) {
			return "";
		}
		return (String) value;
	}

	/**
	 * Sets String into the request
	 * 
	 * @param request
	 * @param parameterName
	 * @param value
	 */
	public static void setRequestAttribute(HttpServletRequest request,
			String parameterName, Object value) {
		request.setAttribute(parameterName, value.toString());
	}

	/**
	 * Searches for the attribute in request and converts it to string
	 * 
	 * @param request
	 * @param parameterName
	 * @return the string if found
	 */
	public static String getRequestAttribute(HttpServletRequest request,
			String parameterName) {
		Object value = request.getAttribute(parameterName);
		if (value == null) {
			return "";
		}
		return (String) value;
	}

	/**
	 * Gets user login
	 * 
	 * @param request
	 * @return user login or null if not authenticated
	 */
	public static String getUserLogin(HttpServletRequest request) {
		try {
			return request.getUserPrincipal().getName();
		} catch (NullPointerException e) {
			return null;
		}
	}

	/**
	 * Generates pages text: 1 ... 3 | 4 | 5 ... 17
	 * 
	 * @param location
	 * @param totalPages
	 * @param pageNow
	 * @return
	 */
	public static String pagesText(String location, int totalPages, int pageNow) {
		String pagesText = "";
		int startPage = 0;
		if (pageNow > 6) {
			startPage = pageNow - 4;
			// first page
			pagesText += pageNumberText(0, pageNow, location);
			pagesText += "| ...";
		}
		int i;
		for (i = startPage; i <= startPage + 10 && i < totalPages; i++) {
			if (i > 0) {
				pagesText += " | ";
			}
			pagesText += pageNumberText(i, pageNow, location);
		}
		if (i < totalPages) {
			// last page
			pagesText += "| ... |";
			pagesText += pageNumberText(totalPages - 1, pageNow, location);
		}
		return pagesText;
	}

	/**
	 * Generates text for one page-number
	 * @param i
	 * @param pageNow
	 * @param location
	 * @return
	 */
	private static String pageNumberText(int i, int pageNow, String location) {
		if (i == pageNow)
			return "<b>" + (i + 1) + "</b>&nbsp;";
		else {
			return "<a href=\"" + location + "?p=" + i + "\">" + (i + 1)
					+ "</a>";
		}
	}
}
